package com.dody.agile.payment.classification;

import com.dody.agile.payment.model.Paycheck;
import com.dody.agile.payment.model.SalesReceipt;
import com.dody.agile.payment.utils.DateUtils;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ShiXin
 * @date 2019/4/11 20:03
 * @description agile
 */
public class CommissionClassification implements PaymentClassification {

    private double lowSalary;

    private double rate;

    private Map<Date,SalesReceipt> salesReceiptMap = new ConcurrentHashMap<>(31);

    public CommissionClassification(double lowSalary) {
        this.lowSalary = lowSalary;
    }

    public CommissionClassification(double lowSalary, double rate) {
        this.lowSalary = lowSalary;
        this.rate = rate;
    }

    public double getRate() {
        return rate;
    }

    public double getSalary() {
        return lowSalary;
    }

    public SalesReceipt getSalesReceipt(Date date) {
        return salesReceiptMap.get(date);
    }

    public void addSalesReceipt(SalesReceipt salesReceiptr) {
         salesReceiptMap.put(salesReceiptr.getDate(),salesReceiptr);
    }


    @Override
    public double calculatePay(Paycheck paycheck) {
        /**
         * 基础薪水 + sum ( 每个销售凭条的销售额* rate )
         * 对于销售， 只计算过去两周的销售凭条
         */
        double commission = 0.0;
        for (SalesReceipt sc : salesReceiptMap.values()) {
            if (DateUtils.between(sc.getDate(), paycheck.getPayPeriodStartDate(),
                    paycheck.getPayPeriodEndDate())) {
                commission += sc.getAmount();
            }
        }
        return lowSalary + commission;
    }
}
